package front

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"qihuang/middleware"
	"qihuang/protocol"
	"qihuang/service/front"
	"strconv"
)

type Families struct {
}

//家庭成员列表
func (Families) GetList(c *gin.Context) {
	resp := &protocol.Resp{Status: 0, Msg: "", Data: ""}
	checkResult := middleware.TokenCheck(c)
	if !checkResult {
		resp.Status = 2
		resp.Msg = "身份验证失败"
		c.JSON(http.StatusOK, resp)
		return
	}
	familyId, err := strconv.Atoi(c.DefaultQuery("family_id", "0"))
	if err != nil || familyId == 0 {
		resp.Status = 1
		resp.Msg = "参数错误"
		c.JSON(http.StatusOK, resp)
		return
	}
	page, err := strconv.Atoi(c.DefaultQuery("page", "1"))
	if err != nil {
		resp.Status = 1
		resp.Msg = "参数错误"
		c.JSON(http.StatusOK, resp)
		return
	}
	pageSize, err := strconv.Atoi(c.DefaultQuery("page_size", "10"))
	if err != nil {
		resp.Status = 1
		resp.Msg = "参数错误"
		c.JSON(http.StatusOK, resp)
		return
	}
	articleServ := front.Families{}
	fields := []string{"id", "relation", "job"}
	resp = articleServ.GetList(familyId, page, pageSize, fields)
	c.JSON(http.StatusOK, resp)
}

//家庭成员详情
func (Families) Show(c *gin.Context) {
	resp := &protocol.Resp{Status: 1, Msg: "", Data: ""}
	checkResult := middleware.TokenCheck(c)
	if !checkResult {
		resp.Status = 2
		resp.Msg = "身份验证失败"
		c.JSON(http.StatusOK, resp)
		return
	}
	id, err := strconv.Atoi(c.DefaultQuery("id", "0"))
	articleServ := front.Families{}
	if err != nil || id <= 0 {
		resp.Status = 1
		resp.Msg = "参数错误"
		c.JSON(http.StatusOK, resp)
		return
	}
	resp = articleServ.GetDetail(id)
	c.JSON(http.StatusOK, resp)
}

//文章类型
func (Families) Update(c *gin.Context) {
	resp := &protocol.Resp{Status: 0, Msg: "", Data: ""}
	checkResult := middleware.TokenCheck(c)
	if !checkResult {
		resp.Status = 2
		resp.Msg = "身份验证失败"
		c.JSON(http.StatusOK, resp)
		return
	}
	Id, err := strconv.Atoi(c.PostForm("id"))
	if err != nil {
		resp.Status = 1
		resp.Msg = "参数类型错误"
		c.JSON(http.StatusOK, resp)
		return
	}
	Relation := c.PostForm("relation")
	Birthday := c.PostForm("birthday")
	Gender := c.PostForm("gender")
	Address := c.PostForm("address")
	Job := c.PostForm("job")
	Height := c.PostForm("height")
	Weight := c.PostForm("weight")
	TasteOne, err := strconv.Atoi(c.PostForm("taste_one"))
	if err != nil {
		resp.Status = -1
		resp.Msg = "参数类型错误"
		c.JSON(http.StatusOK, resp)
		return
	}
	Taste := c.PostForm("taste")
	Smoke, err := strconv.Atoi(c.PostForm("smoke"))
	if err != nil {
		resp.Status = 1
		resp.Msg = "参数类型错误"
		c.JSON(http.StatusOK, resp)
		return
	}
	Wine, err := strconv.Atoi(c.PostForm("wine"))
	if err != nil {
		resp.Status = 1
		resp.Msg = "参数类型错误"
		c.JSON(http.StatusOK, resp)
		return
	}
	Sleep, err := strconv.Atoi(c.PostForm("sleep"))
	if err != nil {
		resp.Status = 1
		resp.Msg = "参数类型错误"
		c.JSON(http.StatusOK, resp)
		return
	}
	FamilyId, err := strconv.Atoi(c.PostForm("family_id"))
	if err != nil {
		resp.Status = 1
		resp.Msg = "参数类型错误"
		c.JSON(http.StatusOK, resp)
		return
	}
	Diseases := c.PostForm("diseases")

	family := front.FamilyParams{
		Relation: Relation,
		Birthday: Birthday,
		Gender:   Gender,
		Address:  Address,
		Job:      Job,
		Height:   Height,
		Weight:   Weight,
		TasteOne: TasteOne,
		Taste:    Taste,
		Smoke:    Smoke,
		Wine:     Wine,
		Sleep:    Sleep,
		FamilyId: FamilyId,
		Diseases: Diseases,
	}
	familyServ := front.Families{}
	resp = familyServ.Update(Id, &family)
	c.JSON(http.StatusOK, resp)
}

//文章类型
func (Families) Create(c *gin.Context) {
	resp := &protocol.Resp{Status: 0, Msg: "", Data: ""}
	checkResult := middleware.TokenCheck(c)
	if !checkResult {
		resp.Status = 2
		resp.Msg = "身份验证失败"
		c.JSON(http.StatusOK, resp)
		return
	}
	Relation := c.PostForm("relation")
	Birthday := c.PostForm("birthday")
	Gender := c.PostForm("gender")
	Address := c.PostForm("address")
	Job := c.PostForm("job")
	Height := c.PostForm("height")
	Weight := c.PostForm("weight")
	TasteOne, err := strconv.Atoi(c.PostForm("taste_one"))
	if err != nil {
		resp.Status = 1
		resp.Msg = "参数类型错误"
		c.JSON(http.StatusOK, resp)
		return
	}
	Taste := c.PostForm("taste")
	Smoke, err := strconv.Atoi(c.PostForm("smoke"))
	if err != nil {
		resp.Status = 1
		resp.Msg = "参数类型错误"
		c.JSON(http.StatusOK, resp)
		return
	}
	Wine, err := strconv.Atoi(c.PostForm("wine"))
	if err != nil {
		resp.Status = 1
		resp.Msg = "参数类型错误"
		c.JSON(http.StatusOK, resp)
		return
	}
	Sleep, err := strconv.Atoi(c.PostForm("sleep"))
	if err != nil {
		resp.Status = 1
		resp.Msg = "参数类型错误"
		c.JSON(http.StatusOK, resp)
		return
	}
	FamilyId, err := strconv.Atoi(c.PostForm("family_id"))
	if err != nil {
		resp.Status = 1
		resp.Msg = "参数类型错误"
		c.JSON(http.StatusOK, resp)
		return
	}
	Diseases := c.PostForm("diseases")

	family := front.FamilyParams{
		Relation: Relation,
		Birthday: Birthday,
		Gender:   Gender,
		Address:  Address,
		Job:      Job,
		Height:   Height,
		Weight:   Weight,
		TasteOne: TasteOne,
		Taste:    Taste,
		Smoke:    Smoke,
		Wine:     Wine,
		Sleep:    Sleep,
		FamilyId: FamilyId,
		Diseases: Diseases,
	}
	familyServ := front.Families{}
	resp = familyServ.Add(&family)
	c.JSON(http.StatusOK, resp)
}

//文章类型
func (Families) Delete(c *gin.Context) {
	resp := &protocol.Resp{Status: 0, Msg: "", Data: ""}
	checkResult := middleware.TokenCheck(c)
	if !checkResult {
		resp.Status = 2
		resp.Msg = "身份验证失败"
		c.JSON(http.StatusOK, resp)
		return
	}
	id, err := strconv.Atoi(c.DefaultQuery("id", "0"))
	if err != nil || id <= 0 {
		resp.Status = 1
		resp.Msg = "参数错误"
		c.JSON(http.StatusOK, resp)
		return
	}
	articleServ := front.Families{}
	resp = articleServ.Delete(id)
	c.JSON(http.StatusOK, resp)
}
